#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 1994  Ling Li
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.


import gtk
from gtk import *

import os

from coralftp_globals import *
from utils import *

RESPONSE_OVERWRITE = 1
RESPONSE_RESUME = 2
RESPONSE_SKIP = 3
RESPONSE_RENAME = 4

class ConfirmFileReplaceDialog(gobject.GObject):

    __gsignals__ = {
        'response' :
        (gobject.SIGNAL_RUN_LAST, gobject.TYPE_NONE, (gobject.TYPE_INT,)),
        }

    __timeout_count = 30

    def __init__(self, coralftp, dire, filename, size1, date1, size2, date2):
        self.__gobject_init__()
        
        self.coralftp = coralftp
        self.__xml = get_glade_xml('confirm_file_replace')
        self.__window = self.__xml.get_widget('confirm_file_replace')
        self.__xml.signal_autoconnect(self)
        self.__filename = filename
        label = self.__xml.get_widget('label_filename')
        label.set_text('This folder already contains a file named\n%s'
                       % filename)
        label = self.__xml.get_widget('label_fileinfo1')
        label.set_text('%d bytes\n%s' % (size1, date1))
        label = self.__xml.get_widget('label_fileinfo2')
        label.set_text('%d bytes\n%s' % (size2, date2))
        
        # if direction is upload resume and resume all is disabled
        from transfer_view import TransferView
        if dire == TransferView.DIRECTION_UP:
            widget = self.__xml.get_widget('btn_resume')
            widget.set_property('sensitive', False)
        #self.__tid = gtk.timeout_add(1000, self.on_timeout)

    def destroy(self):
        #if self.__tid:
        #    gtk.timeout_remove(self.__tid)
        self.__window.destroy()
        return

    def on_timeout(self):
        self.__timeout_count = self.__timeout_count -1
        label = self.__xml.get_widget('label_timeout')
        label.set_text('Delay %d seconds before overwrite.'
                       % self.__timeout_count)
        if self.__timeout_count == 0:
            self.__window.response(RESPONSE_OVERWRITE)
            r = False
        else:
            r = True
        return r

    def on_response(self, obj, resp, *args):
        if resp == RESPONSE_RENAME:
            def on_dialog_response(dlg, resp1, *args):
                value = dlg.value
                dlg.destroy()
                if resp1 == RESPONSE_OK and string.strip(value) != '' and value != None:
                    self.new_name = d.value
                return
            
            from general_input import GeneralInputDialog
            d = GeneralInputDialog('Rename file',
                                   'Please input new name of the file:',
                                   self.__filename)
            d.connect('response', on_dialog_response)
            d.run()
        if resp in (RESPONSE_CANCEL, RESPONSE_OK, RESPONSE_OVERWRITE,
                    RESPONSE_RESUME, RESPONSE_SKIP, RESPONSE_CLOSE):
            self.__window.set_modal(False)
            self.__window.hide()
        self.emit('response', resp)
        return
    
    def run(self):
        self.__window.show()
        self.__window.set_modal(True)
        return

    def __getattr__(self, name):
        if name == 'ask_again':
            widget = self.__xml.get_widget('cb_do_not_ask_again')
            return not widget.get_property('active')
        else:
            raise AttributeError, name

gobject.type_register(ConfirmFileReplaceDialog)

def test_confirm_file_replace1():
    gtk.idle_add(test_confirm_file_replace)
    
def test_confirm_file_replace():
    gtk.threads_enter()
    d = ConfirmFileReplaceDialog(None, 0, '1', 1, '', 1, '')
    while True:
        resp = d.run()
        if resp == RESPONSE_CANCEL:
            d.destroy()
            break
        else:
            print resp
    gtk.threads_leave()

if __name__ == '__main__':
    gtk.threads_init()
    count = 0
    gtk.idle_add(test_confirm_file_replace1)
    gtk.threads_enter()
    gtk.main()
    gtk.threads_leave()
